require 'digest/md5'
class UserController < ApplicationController
  def login
  end

  def verify
    hash_pass = Digest::MD5.hexdigest(params[:user][:userpass])
    user = User.find(:first , :conditions => ["name=? and pass=?",params[:user][:username],hash_pass ] )
    if not user
      #有时候我们手动添加用户到数据库的时候可能添加的md5值中字母是全大写的，这时就需要 .swapcase 来帮忙了
      user = User.find(:first , :conditions => ["name=? and pass=?",params[:user][:username],hash_pass.swapcase ] )
    end
    if user
      session['user'] = user
      redirect_to :controller => 'channels' , :action => 'index'
    else
      flash['notice'] = '用户名或密码错误'
      redirect_to :controller => 'user' , :action => 'login'
    end
  end

  def logout
    reset_session
    redirect_to :controller => 'wap'
  end

end